#coding:utf-8
import time
from multiprocessing import Process
from proxyAPI import app
from proxyGetter import Getter
from proxyTester import Tester

TESTER_CYCLE = 50
GETTER_CYCLE = 500
TESTER_ENABLED = True
GETTER_ENABLED = True
API_ENABLED = True
API_HOST = '0.0.0.0'
API_PORT = '5555'

class Scheduler:
    def schedule_tester(self, cycle=TESTER_CYCLE):
        '''
            定时测试代理服务
        '''
        tester = Tester()
        while True:
            print('测试器开始运行..')
            tester.run()
            time.sleep(cycle)
    
    def schedule_getter(self, cycle=GETTER_CYCLE):
        '''
            定时获取代理服务
        '''
        getter = Getter()
        while True:
            print('开始抓取代理..')
            getter.run()
            time.sleep(cycle)

    def schedule_api(self):
        '''
            开启外部获取api
        '''
        app.run(API_HOST, API_PORT)
    
    def run(self):
        '''
            所有的功能一键启动
        '''
        print('代理池开始运行..')
        if TESTER_ENABLED:
            tester_process = Process(target=self.schedule_tester)
            tester_process.start()

        if GETTER_ENABLED:
            getter_process = Process(target=self.schedule_getter)
            getter_process.start()

        if API_ENABLED:
            api_process = Process(target=self.schedule_api)
            api_process.start()

if __name__ == '__main__':
    Scheduler().run()